#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

NCPU=$(shell grep -c processor /proc/cpuinfo)

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

builddir = release
builddirdebug = debug
rpath='$$ORIGIN/../lib/private'
originrpath='$$ORIGIN'

override_dh_auto_configure:

ifeq "$(DEB_DUMMY)" ""
	( test -d $(builddirdebug) || mkdir $(builddirdebug) ) && cd $(builddirdebug) && \
	cmake .. -DBUILD_CONFIG=xtrabackup_release -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=/usr \
	  -DWITH_SSL=system -DINSTALL_MYSQLTESTDIR=/usr/share/percona-xtrabackup-test-91 \
	  -DINSTALL_MANDIR=/usr/share/man -DWITH_MAN_PAGES=1 -DMINIMAL_RELWITHDEBINFO=OFF \
	  -DDOWNLOAD_BOOST=1 -DWITH_BOOST=libboost -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
	  -DINSTALL_PLUGINDIR=lib/xtrabackup/plugin -DFORCE_INSOURCE_BUILD=1 -DWITH_ZLIB=bundled -DWITH_ZSTD=bundled -DWITH_PROTOBUF=bundled && \
	cd ..
	( test -d $(builddir) || mkdir $(builddir) ) && cd $(builddir) && \
	cmake .. -DBUILD_CONFIG=xtrabackup_release -DCMAKE_INSTALL_PREFIX=/usr \
	  -DWITH_SSL=system -DINSTALL_MYSQLTESTDIR=/usr/share/percona-xtrabackup-test-91 \
	  -DINSTALL_MANDIR=/usr/share/man -DWITH_MAN_PAGES=1 -DMINIMAL_RELWITHDEBINFO=OFF \
	  -DDOWNLOAD_BOOST=1 -DWITH_BOOST=libboost -DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
	  -DINSTALL_PLUGINDIR=lib/xtrabackup/plugin -DFORCE_INSOURCE_BUILD=1 -DWITH_ZLIB=bundled -DWITH_ZSTD=bundled -DWITH_PROTOBUF=bundled
else
	# Dummy binaries that avoid compilation
	echo 'main() { return 300; }' | gcc -x c - -o xtrabackup
	echo 'main() { return 300; }' | gcc -x c - -o xtrabackup_55
	echo 'main() { return 300; }' | gcc -x c - -o xtrabackup_56
	echo 'main() { return 300; }' | gcc -x c - -o xbstream
	echo 'main() { return 300; }' | gcc -x c - -o xbcrypt
	echo 'main() { return 300; }' | gcc -x c - -o xbcloud
endif

	#docbook-to-man debian/xtrabackup.sgml > xtrabackup.1
override_dh_auto_build:
	cd $(builddirdebug) && make -j$(NCPU) && cd ..
	cd $(builddir) && make -j$(NCPU)

override_dh_builddeb:
	dh_builddeb -- -Zgzip

override_dh_auto_install:
	@echo "RULES.$@"
	# complete install first
	cd $(builddir) && \
	make install DESTDIR=../debian/tmp

override_dh_install:
	dh_install
	mkdir -p debian/percona-xtrabackup-91/usr/lib/private/
	mkdir -p debian/percona-xtrabackup-91/usr/bin/
	cp ./$(builddir)/library_output_directory/libprotobuf-lite.so* debian/percona-xtrabackup-91/usr/lib/private/
	cp ./$(builddirdebug)/runtime_output_directory/xtrabackup debian/percona-xtrabackup-91/usr/bin/xtrabackup-debug
	patchelf --debug --set-rpath $(originrpath) debian/percona-xtrabackup-91/usr/lib/private/libprotobuf-lite.so*
	patchelf --debug --set-rpath $(rpath) debian/percona-xtrabackup-91/usr/bin/xtrabackup-debug
	@echo "RULES.$@"

override_dh_strip:
	dh_strip --dbg-package=percona-xtrabackup-dbg-91
	dh_strip -Xlibprotobuf-lite

%:
	dh $@

